package com.googlecode.gaal.data.impl;

import com.googlecode.gaal.data.api.IntSequence;

public abstract class AbstractSequence implements IntSequence {

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        for (int i = 0; i < size(); i++)
            result = prime * result + get(i);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        IntSequence other = (IntSequence) obj;
        return compareTo(other) == 0;
    }

    @Override
    public int compareTo(IntSequence other) {
        for (int i = 0; i < size() && i < other.size(); i++) {
            if (get(i) != other.get(i))
                return get(i) - other.get(i);
        }
        return size() - other.size();
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        boolean isFirst = true;
        for (int i = 0; i < size(); i++) {
            if (isFirst)
                isFirst = false;
            else
                sb.append(", ");
            sb.append(get(i));
        }
        sb.append(']');
        return sb.toString();
    }

}
